package com.xlzcx.xltz.controller;

import cn.hutool.json.JSONObject;
import com.xlzcx.xltz.entity.vo.AStandingVo;
import com.xlzcx.xltz.filter.SkipLogin;
import com.xlzcx.xltz.service.a_standing.AStandingService;
import com.xlzcx.xltz.service.d_region.DRegionService;
import com.xlzcx.xltz.utils.R;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * 作者: 李咨辰
 * 文件名: AStandingController.java
 * 路径: com.xlzcx.xltz.controller
 * 时间: 2024-11-18 13:25
 * 版本: 1.0
 * 描述: TODO 身份信息
 **/
@RestController
@RequestMapping("/standing")
public class AStandingController {
    @Resource
    private AStandingService aStandingService;
    @Resource
    private DRegionService dRegionService;

    /**
     * 新建实名信息
     *
     * @param aStanding 实名对象
     * @return id
     */
    @SneakyThrows
    @SkipLogin
    @PostMapping("/addStanding")
    public R addStanding(@RequestBody AStandingVo aStanding, HttpServletRequest request) {
        //检验userId是否为空
        if (aStanding.getUserId() != null && aStanding.getUserId() != 0L) {
            //验证姓名是否符合中国名字标准
            if (Pattern.compile("^[\\u4e00-\\u9fa5]{2,4}(?:[·・][\\u4e00-\\u9fa5]{2,4})?").matcher(aStanding.getName()).matches()) {
                //判断身份证长度是否正确
                if (aStanding.getIdNumber().length() == 18) {
                    //获取第18位校验码
                    String verify = aStanding.getIdNumber().substring(17, 18);
                    //如果第18位校验码为小写x则转换成大写X
                    if (verify.equals("x")) {
                        aStanding.setIdNumber(aStanding.getIdNumber().substring(0, 17) + "X");
                        verify = "X";
                    }
                    //校验前17为是否为纯数字,第18为校验码是否为纯数字或X
                    if (Pattern.compile("^\\d+$").matcher(verify).matches() || verify.equals("X") &&
                            (Pattern.compile("^\\d+$").matcher(aStanding.getIdNumber().substring(0, 17)).matches())) {
                        //验证第17位性别编码是否正确
                        int sex = Integer.parseInt(aStanding.getIdNumber().substring(16, 17));
                        if (((sex % 2 == 0) && aStanding.getSex() == 1) || (sex % 2 != 0 && aStanding.getSex() == 0)) {
                            //验证第7-14位出生日期是否正确
                            String birthTime = aStanding.getIdNumber().substring(6, 14);
                            if (new SimpleDateFormat("yyyyMMdd").parse(birthTime).getTime() < (new Date().getTime() - (1000 * 60 * 60 * 48))) {
                                //验证第18位校验码是否正确
                                //前17位对应的系数
                                int[] product = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                                //乘积集合
                                int us = 0;
                                for (int i = 0; i < product.length; i++) {
                                    us += Integer.parseInt(aStanding.getIdNumber().substring(i, i + 1)) * product[i];
                                }
                                //余数对应关系
                                String[] relation = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                                //判断校验码是否正确
                                if (relation[us % 11].equals(verify)) {
                                    //获取地域编码
                                    String adcode = aStanding.getIdNumber().substring(0, 6);
                                    //初步验证1-6位地理编码是否正确
                                    if (!adcode.startsWith("00") && !adcode.startsWith("00", 2)
                                            && !adcode.startsWith("00", 4) && dRegionService.isExist(adcode += "000", "district")) {
                                        return aStandingService.addStanding(aStanding, request);
                                    }
                                }
                            }
                        }
                    }
                }
                return R.error("身份证格式不正确！");
            }
            return R.error("姓名格式不正确！");
        }
        return R.error("请先登录！");
    }

}
